﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BuildHelper.Scheduler
{
    public partial class ZadanieParamsControl : DevExpress.XtraEditors.XtraUserControl
    {
        private ProgNetComponentsDx.Mode.ModeProvider _OtherModeProvider;

        public ProgNetComponentsDx.Mode.ModeProvider OtherModeProvider
        {
            get { return _OtherModeProvider; }
            set
            {
                if (!this.IsDesignMode())
                {
                    if (_OtherModeProvider != null)
                        _OtherModeProvider.CurrentModeChanged -= new EventHandler(_OtherModeProvider_CurrentModeChanged);
                }
                _OtherModeProvider = value;
                if (!this.IsDesignMode())
                {
                    if (_OtherModeProvider != null)
                        _OtherModeProvider.CurrentModeChanged += new EventHandler(_OtherModeProvider_CurrentModeChanged);
                }
            }
        }

        void _OtherModeProvider_CurrentModeChanged(object sender, EventArgs e)
        {
            modeProvider1.CurrentMode = OtherModeProvider.CurrentMode;
        }

        public ZadanieParamsControl()
        {
            InitializeComponent();
        }

        public DataRow Row
        {
            get;
            set;
        }

        public string ErrorMsg
        {
            get;
            set;
        }
        
        public virtual bool ValidateAll()
        {
            ErrorMsg = "";
            string err = "";
            dxErrorProvider1.ClearErrors();
            bool result = modeProvider1.ValidateRequiredEdits(ref err);
            ErrorMsg = err;
            return result;
        }
        
        public virtual void SetDefault()
        {
            foreach (Control c in Controls)
            {
                if (c is BaseEdit)
                {
                    if (c is CheckEdit)
                    {
                        if (c.Text.XMatch("Pokaż info"))
                            (c as CheckEdit).Checked = true;
                        else
                            (c as CheckEdit).Checked = false;
                    }
                    else if (c is LookUpEdit && browserProvider1.GetSettings(c) != null && browserProvider1.GetSettings(c).BrowserId.IsTheSame(Init.InitFormBrowserNames.Setup.Brancze))
                    {
                        (c as BaseEdit).EditValue = BuildHelper.Setup.Brancze.BranczeTools.ActiveBranch;
                    }
                    else
                        (c as BaseEdit).EditValue = DBNull.Value;
                    OnEditValueChanged(c);
                }
            }
        }

        public virtual void SetControlsFromRow(Zadanie z)
        {
            BaseEdit edit;
            if (z != null && Row!=null)
            {
                if (z.Parameters != null)
                {
                    int no;
                    for (int i = 0; i < z.Parameters.Length; i++)
                    {
                        no = i + 1;
                        edit = GetEdit(no);
                        edit.EditValue = z.ConvertParam(Row["WRK_ZAD_PARAM" + no].AsString(), no);
                    }
                }
            }
        }
        
        public event EventHandler EditValueChanged;
       
        protected void OnEditValueChanged(object sender)
        {
            if (EditValueChanged != null)
                EditValueChanged(sender, EventArgs.Empty);
            if (Row != null)
            {
                BaseEdit edit = sender as BaseEdit;
                Row["WRK_ZAD_PARAM" + edit.Tag.AsString()] = edit.EditValue.AsString();
            }
        }

        public virtual void InitEvents()
        {
            foreach (Control c in this.Controls)
            {
                if (c is BaseEdit)
                {
                    (c as BaseEdit).EditValueChanged += new EventHandler(ZadanieParamsControl_EditValueChanged);
                }
            }
        }

        void ZadanieParamsControl_EditValueChanged(object sender, EventArgs e)
        {
            OnEditValueChanged(sender);
        }

        public BaseEdit GetEdit(int no)
        {
            foreach (Control c in Controls)
            {
                if (c is BaseEdit && c.Tag.AsString() == no.AsString())
                    return c as BaseEdit;
            }
            return null;
        }

        public virtual BHTasks.BaseTask GetTask()
        {
            return null;
        }
    }
}
